package Scanner.Test;

import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
import java.io.File;

import jxl.Workbook;
import jxl.Cell;
import jxl.CellType;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;

import jxl.read.biff.BiffException;

public class new4 extends JFrame {
    DefaultTableModel model = new DefaultTableModel();
    JScrollPane scrollPane = new JScrollPane();
    JTable table;

    public new4() {
        setLayout(new BorderLayout());
        model = new DefaultTableModel(new Object[][]{{null, null, null, null},
                {null, null, null, null}},
                new String[]{"分类号", "资产名称", "规格", "型号", "单价", "数量", "总金额",
                });
//model.setRowCount(0);// 清除原有行
        table = new JTable(model);
        table.setRowHeight(20);
        JScrollPane sp = new JScrollPane(table);
        add(sp, BorderLayout.CENTER);
        read();
        setTitle("侧签打印");
        setSize(1000, 600);
        setVisible(true);
    }

    public void read() {
        Object[] object = new Object[table.getColumnCount()];
        try {
            Workbook book = Workbook.getWorkbook(new File("res.xls"));
            //获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            //得到第一列第一行的单元格
            int rows = sheet.getRows();
            int column = sheet.getColumns();
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < column; j++) {
                    /// Cell cell1 = sheet.getCell(j, i);
                    // String result = cell1.getContents();
                    // System.out.print(result+",");
                    Cell c00 = sheet.getCell(j, i);
                    if (c00.getType() == CellType.LABEL) {
                        LabelCell labelc00 = (LabelCell) c00;
                        object[j] = labelc00.getString();
                        //jTable.setValueAt(labelc00.getString(), i-1, j);
                    } else if (c00.getType() == CellType.NUMBER) {
                        Double numd;
                        int numi;
                        NumberCell numc10 = (NumberCell) c00;
                        numd = new Double(numc10.getValue());
                        numi = numd.intValue();
                        object[j] = numi;
//    jTable.setValueAt(numi, i-1, j);
                    }
                }
                model.addRow(object);
                System.out.println(object[1] + "   " + object[3] + "   " + object[4] + "   " + object[5] + "   " + object[6] + "   " + object[12]);
                System.out.println("-----------------------------");
            }
            book.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String args[]) {
        new new4();
    }
}
